package com.spa.modules.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.common.api.vo.Result;
import com.spa.common.utils.RedisUtil;
import com.spa.modules.spa.entity.SpaMenu;
import com.spa.modules.spa.mapper.SpaMenuMapper;
import com.spa.modules.system.entity.SpaUserRole;
import com.spa.modules.system.mapper.SpaUserRoleMapper;

/**
 * 描述: 角色
 * author: P0081447
 * date: 2022-06-27 17:25:43
 */
@Service
public class SpaUserRoleService {

    @Autowired
    private SpaUserRoleMapper spaUserRoleMapper;
    @Autowired
    private SpaMenuMapper spaMenuMapper;
    @Autowired
    private RedisUtil redisUtil;
    
    /**
     * 角色权限码集合放入
     */
    public void setPermissionListToRedis() {
        List<SpaMenu> spaMenuList = spaMenuMapper.selectList(null);
        Map<String, SpaMenu> menuPermissionMap = new HashMap<String, SpaMenu>();
        for (SpaMenu spaMenu : spaMenuList) {
            menuPermissionMap.put(spaMenu.getId().toString(), spaMenu);
        }
        List<SpaUserRole> spaUserRoleList = spaUserRoleMapper.selectList(null);
        for (SpaUserRole spaUserRole : spaUserRoleList) {
            List<String> menuList = JSON.parseArray(spaUserRole.getMenu(), String.class);
            if(menuList.size() > 0) {
                List<String> menuPermissionList = new ArrayList<String>();
                List<SpaMenu> roleMenuList = new ArrayList<SpaMenu>();
                for (String menuId : menuList) {
                    menuPermissionList.add(menuPermissionMap.get(menuId).getPermission());
                    roleMenuList.add(menuPermissionMap.get(menuId));
                }
                redisUtil.set("menuPermission:" + spaUserRole.getId(), JSON.toJSONString(menuPermissionList));
                redisUtil.set("roleMenu:" + spaUserRole.getId(), JSON.toJSONString(roleMenuList));
            }
            
        }
    }
    
    /**
     * 分页列表查询
     *
     * @param spaUserRole
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Result<Object> queryPageList(HttpServletRequest req, Integer pageNo, Integer pageSize){
        Page<SpaUserRole> page= new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpaUserRole> queryWrapper = new LambdaQueryWrapper<>();
        Page<SpaUserRole> articlePage = spaUserRoleMapper.selectPage(page, queryWrapper);
        List<SpaUserRole> records = articlePage.getRecords();
        return Result.resultOK(records);
    }
    
    /**
     * 获取全部角色信息
     * 
     * @return
     */
    public Result<Object> getAllRoleList(){
        List<SpaUserRole> spaUserRoleList = spaUserRoleMapper.selectList(null);
        return Result.resultOK(spaUserRoleList);
    }
    
    /**
     * 添加
     *
     * @param testTabel
     * @return
     */
    @Transactional
    public Result<Object> insert(SpaUserRole spaUserRole) {
        spaUserRoleMapper.insert(spaUserRole);
        return Result.resultOK("添加成功！");
    }
    
    /**
     * 编辑
     *
     * @param spaUserRole
     * @return
     */
    @Transactional
    public Result<Object> saveById(SpaUserRole spaUserRole) {
        spaUserRoleMapper.updateById(spaUserRole);
        return Result.resultOK("编辑成功!");
    }
    
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Transactional
    public Result<Object> deleteById(Long id) {
        spaUserRoleMapper.deleteById(id);
        return Result.resultOK("删除成功!");
    }
    
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public Result<Object> selectById(Long id) {
        SpaUserRole spaUserRole = spaUserRoleMapper.selectById(id);
        return Result.resultOK(spaUserRole);
    }
}